<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';
require APPPATH . 'core/solr/news.php';

/**
 * Search news
 *
 * @author phuchv
 */
class search extends CI_Controller {

    protected $data;
    protected $num_records = 0;

    function __construct() {
        $this->_initdata();
    }

    function _initdata() {
        parent::__construct();
        session_start();

        $this->load->model('Frontendmodel');
        $this->load->model('search_model');
        $this->load->helper('eximcached');
        $this->load->library('javascript');
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['page_header'] = "header";
        $this->data['page_rightbar'] = "rightbar";
        $this->data['page_footer'] = "footer";
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);

        $news_cate = "van-ban-phap-quy";
        $news_cate_id = NEWS_CATE_VBPQ;
        list($this->data['aNews2']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
        $this->data['news_cate_2'] = $news_cate;

        //get News list (cateid=3)
        $news_cate = "kien-thuc-xnk";
        $news_cate_id = NEWS_CATE_KTXNK;
        list($this->data['aNews3']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
        $this->data['news_cate_3'] = $news_cate;

        //get News list (cateid=4)
        $news_cate = "thu-tuc-hai-quan";
        $news_cate_id = NEWS_CATE_TTHQ;
        list($this->data['aNews4']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
        $this->data['news_cate_4'] = $news_cate;

        //get News list (cateid=5)
        $news_cate = "nghiep-vu-giao-nhan";
        $news_cate_id = NEWS_CATE_NVGN;
        list($this->data['aNews5']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
        $this->data['news_cate_5'] = $news_cate;

        //get News list (cateid=6)
        $news_cate = "nghiep-vu-khac";
        $news_cate_id = NEWS_CATE_NVK;
        list($this->data['aNews6']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
        $this->data['news_cate_6'] = $news_cate;
    }

    function index() {
        //get City list
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        //get current url
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];

        $filterType = 1;
        $cateId = 0;
        if ($_POST) {

            $keyword = sanitize_string($_POST['keyword']);

            //vi tri tim kiem
            $filterType = $this->input->post('chkAll') | $this->input->post('chkTitle') | $this->input->post('chkShortDesc') | $this->input->post('chkFullDesc');
            if ($filterType == 0)
                $filterType = 1;

            //pham vi tim kiem    
            $this->data['chkAll'] = false;
            $this->data['chkTitle'] = false;
            $this->data['chkShortDesc'] = false;
            $this->data['chkFullDesc'] = false;
            if ($this->input->post('chkAll')) {
                $this->data['chkAll'] = true;
            }
            if ($this->input->post('chkTitle')) {
                $this->data['chkTitle'] = true;
            }
            if ($this->input->post('chkShortDesc')) {
                $this->data['chkShortDesc'] = true;
            }
            if ($this->input->post('chkFullDesc')) {
                $this->data['chkFullDesc'] = true;
            }
            if (!$this->input->post('chkTitle') && !$this->input->post('chkShortDesc') && !$this->input->post('chkFullDesc')) {
                $this->data['chkAll'] = true;
            }
            if ($this->input->post('optCate') != "0") {
                $cateId = $this->input->post('optCate');
            }
            $this->data['filterType'] = $filterType;
            $this->data['cateId'] = $cateId;
        } else {
            $keyword = str_replace("-", " ", $this->uri->segment(2));
            $filterType = $this->uri->segment(5);
            $cateId = $this->uri->segment(6);
            $this->data['filterType'] = $filterType;
            $this->data['cateId'] = $cateId;
        }

        if (isset($keyword) === true && trim($keyword) !== '') {

            $keyword = urldecode($keyword);

            $this->paging_search($keyword, 0, 0, $filterType, $cateId);
            //data for layout.
            $this->data['page_title'] = "Nhập nội dung cần tìm";
            $this->data['page_css'] = "search";
            $this->data['keyword'] = $keyword;
            $this->data['page_name'] = "search";
            $this->data['search'] = 1;
            $this->data['previous_id'] = 1;
            $this->data['msg'] = "";
            $this->data['page_nodata'] = "Không tìm thấy dữ liệu";
            $this->data['num_records'] = $this->num_records;
        } else {
            $this->data['num_records'] = 0;
            $this->data['aNews'] = null;
        }

        //data for layout.
        $this->data['page_title'] = "Nhập nội dung cần tìm";
        $this->data['page_css'] = "search";
        $this->data['keyword'] = $keyword;
        $this->data['page_name'] = "search";
        $this->data['search'] = 1;
        $this->data['previous_id'] = 1;
        $this->data['msg'] = "";
        $this->data['page_nodata'] = "Không tìm thấy dữ liệu";
        $this->data['msg'] = "";

        $this->show($this->data);
    }

    function paging_search($keyword, $offset = 0, $totalRecords = 0, $filterType = 1, $cateId = 0) {
        $keyword = urldecode($keyword);
        if (isset($keyword) === true && trim($keyword) !== '') {

            $index = (int) ($offset / PAGESIZE);
            $currLimit = (int) $totalRecords - $offset;
            if ($totalRecords == 0 || $totalRecords - $offset >= PAGESIZE) {
                $limit = PAGESIZE;
            } else {
                $limit = $currLimit;
            }

            if (SOLR_ENABLED == "true") {
                $clsNews = new news_solr();
                list($this->num_records, $aNews) = $clsNews->select($keyword, $index, $limit, $filterType, $cateId);
            } else {
                $clsDAO = new search_model();
                list($this->num_records, $aNews) = $clsDAO->paging_search($keyword, $index, $limit, $filterType, $cateId);
            }

            $this->data['aNews'] = $aNews;
            $this->data['keyword'] = $keyword;

            if (!$_POST)
                $this->load->view('search_item', $this->data);
        }
    }

}

?>
